/*
 * GraphUtil.h
 *
 *  Created on: 2011-8-5
 *      Author: terry
 */

#ifndef GRAPHUTIL_H_
#define GRAPHUTIL_H_



#include <windows.h>
#include <tchar.h>

#pragma warning( disable : 4995 )
#include <DShow.h>
#include <deque>
#include <vector>
#include <string>



class GraphUtil
{
public:
	GraphUtil();
	~GraphUtil();

	enum Constant
	{
		PINDIR_NONE = 3
	};

	typedef std::wstring					WString;
	typedef std::vector< std::wstring >		WStringArray;

    // aux
    static HRESULT getFriendlyName(IMoniker* pMoniker, VARIANT& var);
    static HRESULT getFriendlyName(IMoniker* pMoniker, WString& wstr);

	static HRESULT enumSystemDevice(REFCLSID clsidDeviceClass, WStringArray& array );

	static HRESULT addGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister);
    static void removeGraphFromRot(DWORD pdwRegister);

	static HRESULT saveGraphFile(IGraphBuilder *pGraph, LPCWSTR	wszPath);
    static HRESULT loadGraphFile(IGraphBuilder *pGraph, LPCWSTR wszName);
    
    static void deleteMediaType( AM_MEDIA_TYPE *pmt );
    static void freeMediaType( AM_MEDIA_TYPE& mt);

    // create
    static HRESULT createFilter(REFCLSID rclsid, IBaseFilter **ppFilter);

	static HRESULT createFilter(const CLSID clsidDeviceClass, LPCWSTR szDevName,
                IBaseFilter **ppFilter);

    static HRESULT createFilter(const CLSID clsidDeviceClass, LPCWSTR szDevName,
                int nIndex, IBaseFilter **ppFilter);

    // remove
    static void removeAllFilters( IGraphBuilder* pBuilder );
    static HRESULT removeFilter(IGraphBuilder *pGB,LPCWSTR strName);

    // disconnect
    static HRESULT disconnectPin(IGraphBuilder *pGB,IPin* pPin);
    static HRESULT disconnectFilter(IGraphBuilder* pGB,IBaseFilter* pFilter);
    static HRESULT disconnectAll(IGraphBuilder* pGB);

    
    // connect
	static HRESULT connectPin( IGraphBuilder* pGB,IPin* pPinSrc,
            IPin* pPinDest, bool direct = false,
            const AM_MEDIA_TYPE *pmt = NULL);

	static HRESULT connectPin(IGraphBuilder* pGB,IBaseFilter *pFilterSrc, int nSrcPin, IPin *pPinDest,
            bool direct = false,
            const AM_MEDIA_TYPE *pmt = NULL );

	static HRESULT connectPin(IGraphBuilder* pGB, IPin *pPinSrc, IBaseFilter *pFilterDest,
            int nDestPin, bool direct = false,
            const AM_MEDIA_TYPE *pmt = NULL);

	static HRESULT connectFilter(IGraphBuilder* pGB,IBaseFilter *pFilterSrc, IBaseFilter *pFilterDest,
			int nSrcPin = 0, int nDestPin = 0,
			bool direct = false,
            const AM_MEDIA_TYPE *pmt = NULL);
            

    // find
    static HRESULT findFilterByRealName( IGraphBuilder *pGB, LPCWSTR strName,
				IBaseFilter** ppFilter);

	static HRESULT findRenderer(IGraphBuilder *pGB, const GUID *mediatype,
				IBaseFilter **ppFilter);

	static HRESULT findAudioRenderer(IGraphBuilder *pGB, IBaseFilter **ppFilter);

    static HRESULT findVideoRenderer(IGraphBuilder *pGB, IBaseFilter **ppFilter);

    static HRESULT countFilterPins(IBaseFilter *pFilter, ULONG *pulInPins, ULONG *pulOutPins);

    static HRESULT getPin( IBaseFilter * pFilter, PIN_DIRECTION dirrequired,
				int iNum, IPin **ppPin );
	static IPin* getInPin( IBaseFilter * pFilter, int nPin );
    static IPin* getOutPin( IBaseFilter * pFilter, int nPin );

	static HRESULT getPin( IBaseFilter * pFilter, PIN_DIRECTION dirrequired,
                LPCWSTR strName, IPin **ppPin);
    static IPin* getInPin(IBaseFilter * pFilter,LPCWSTR strName);
    static IPin* getOutPin(IBaseFilter * pFilter,LPCWSTR strName);

	static HRESULT getUnconnectedPin( IBaseFilter *pFilter, PIN_DIRECTION PinDir,
				IPin **ppPin);


	static HRESULT getPinByMajorType( IBaseFilter * pFilter, GUID guid,
				PIN_DIRECTION dirrequired, IPin** ppPin );

    static HRESULT getPin(IBaseFilter* pFilter, PIN_DIRECTION pinDir, WStringArray& arrName);

    // pin
    static WString getPinName( IPin* pPin );
	static HRESULT getConnectedFilter(IPin* pSrcPin,IBaseFilter** ppFilter);
	static bool isPinConnected( IPin* pPin );
	static HRESULT getPinMedia(IPin* pPin,GUID guid,BOOL bExcept,AM_MEDIA_TYPE** ppMedia);

    //
    static bool isSupportPropertyPage(IBaseFilter *pFilter);
    static HRESULT showFilterPropertyPage(IBaseFilter *pFilter, HWND hwndParent);

	static bool writeImage( BYTE* pDib, LPCTSTR filename );
	static HRESULT grab( IVMRWindowlessControl* pControl, LPCTSTR filename );
    static HRESULT grab( IBasicVideo* pBasicVideo, LPCTSTR strFile );
    static WString getFilterFile(REFCLSID clsid);

    static HRESULT getVideoInfo(AM_MEDIA_TYPE* pmt, long& width, long& height, LONGLONG& timePerFrame);
    static HRESULT setVideoInfo(AM_MEDIA_TYPE* pmt, long width, long height, LONGLONG timePerFrame);

};

#endif /* GRAPHUTIL_H_ */
